package com.yfjin.h264.tcrtc.listener;

import android.os.Bundle;
import android.util.Log;

import com.tencent.trtc.TRTCCloudDef;
import com.tencent.trtc.TRTCCloudListener;
import com.tencent.trtc.TRTCStatistics;

import java.util.ArrayList;

public class CloudListenerDelegate extends TRTCCloudListener {

    private String TAG = CloudListenerDelegate.class.getName();

//    private ControlCmdCallback mControlCmdCallback;
//    public void setControlCmdCallback(ControlCmdCallback mControlCmdCallback) {
//        this.mControlCmdCallback = mControlCmdCallback;
//    }

    private RoomInfoCallback mRoomInfoCallback;

    public void setRoomInfoCallback(RoomInfoCallback mRoomInfoCallback) {
        this.mRoomInfoCallback = mRoomInfoCallback;
    }

    @Override
    public void onRecvCustomCmdMsg(String userId, int cmdID, int seq, byte[] message) {
        Log.i(TAG, "onRecvCustomCmdMsg:接收到其他用户发送的自定义消息-----> " + new String(message));
//        ControlCmd cmd= ControlCmd.getCmd(cmdID);
//        if (cmd!=null){
//            cmd.onAction(message);
//        }
    }

    @Override
    public void onMissCustomCmdMsg(String userId, int cmdID, int errCode, int missed) {
        Log.i(TAG, "onMissCustomCmdMsg: 自定义消息丢失-----》" + cmdID);
    }

    @Override
    public void onRecvSEIMsg(String userId, byte[] data) {
        Log.i(TAG, "onRecvSEIMsg: 收到SEI自定义消息------》" + new String(data));
    }

    @Override
    public void onError(int errCode, String errMsg, Bundle extraInfo) {
        Log.i(TAG, "onError: TRTC出错--->" + errCode + ":" + errMsg);
        if (mRoomInfoCallback != null) {
            mRoomInfoCallback.onError(errCode);
        }
    }

    @Override
    public void onWarning(int warningCode, String warningMsg, Bundle extraInfo) {
        Log.i(TAG, "onWarning: TRTC警告---》" + warningMsg);
    }

    @Override
    public void onEnterRoom(long result) {
        //result >0 Enter room succeed
        Log.i(TAG, "onEnterRoom: 进入房间result---->" + result);
        if (mRoomInfoCallback != null) {
            mRoomInfoCallback.onEnterResult(result);
        }
    }

    @Override
    public void onExitRoom(int reason) {
        Log.i(TAG, "onExitRoom: 离开房间");
    }

    @Override
    public void onSwitchRole(int errCode, String errMsg) {
        /*调用 TRTCCloud 中的 switchRole() 接口会切换主播和观众的角色，该操作会伴随一个线路切换的过程， 待 SDK 切换完成后，会抛出 onSwitchRole() 事件回调。*/
        Log.i(TAG, "onSwitchRole: 切换角色的事件回调");
    }

    @Override
    public void onSwitchRoom(int errCode, String errMsg) {
        Log.i(TAG, "onSwitchRoom: 切换房间的结果回调");
    }

    @Override
    public void onConnectOtherRoom(String userId, int errCode, String errMsg) {
        Log.i(TAG, "onConnectOtherRoom: 请求跨房通话的结果回调");
    }

    @Override
    public void onDisConnectOtherRoom(int errCode, String errMsg) {
        Log.i(TAG, "onDisConnectOtherRoom: 结束跨房通话的结果回调");
    }

    int loginCount = 0;

    @Override
    public void onRemoteUserEnterRoom(String userId) {
        loginCount++;
        Log.i(TAG, "onRemoteUserEnterRoom: 有用户加入当前房间------>" + userId);
    }

    @Override
    public void onRemoteUserLeaveRoom(String userId, int reason) {
        --loginCount;
        Log.i(TAG, "onRemoteUserLeaveRoom: 有用户离开当前房间------>" + userId + "--loginCount:" + loginCount);
//        if (loginCount <= 1) {
        /*关闭*/
        if (mRoomInfoCallback != null) {
            mRoomInfoCallback.onNoUser();
        }
//        }
    }

    @Override
    public void onUserVoiceVolume(ArrayList<TRTCCloudDef.TRTCVolumeInfo> userVolumes, int totalVolume) {
        Log.i(TAG, "onUserVoiceVolume: 某远端用户发布/取消了主路视频画面");
    }

    @Override
    public void onUserSubStreamAvailable(String userId, boolean available) {
        Log.i(TAG, "onUserSubStreamAvailable: 某远端用户发布/取消了辅路视频画面");
    }

    @Override
    public void onUserAudioAvailable(String userId, boolean available) {
        Log.i(TAG, "onUserAudioAvailable: 某远端用户发布/取消了自己的音频");
    }

    @Override
    public void onFirstVideoFrame(String userId, int streamType, int width, int height) {
        Log.i(TAG, "onFirstVideoFrame: SDK 开始渲染自己本地或远端用户的首帧画面");
    }

    @Override
    public void onFirstAudioFrame(String userId) {
        Log.i(TAG, "onFirstAudioFrame: SDK 开始播放远端用户的首帧音频");
    }

    @Override
    public void onSendFirstLocalVideoFrame(int streamType) {
        Log.i(TAG, "onSendFirstLocalVideoFrame: 自己本地的首个视频帧已被发布出去");
    }

    @Override
    public void onSendFirstLocalAudioFrame() {
        Log.i(TAG, "onSendFirstLocalAudioFrame: 自己本地的首个音频帧已被发布出去");
    }

    @Override
    public void onRemoteVideoStatusUpdated(String userId, int streamType, int status, int reason, Bundle extraInfo) {
        Log.i(TAG, "onRemoteVideoStatusUpdated: 远端视频状态变化的事件回调");
    }

    @Override
    public void onNetworkQuality(TRTCCloudDef.TRTCQuality localQuality, ArrayList<TRTCCloudDef.TRTCQuality> remoteQuality) {
        Log.i(TAG, "onNetworkQuality: 网络质量的实时统计回调");
    }

    @Override
    public void onStatistics(TRTCStatistics statistics) {
        Log.i(TAG, "onStatistics: 音视频技术指标的实时统计回调--》" + statistics.rtt);
        if (mRoomInfoCallback != null) {
            mRoomInfoCallback.onFpsInfo(statistics.rtt);
        }
    }


    @Override
    public void onSpeedTestResult(TRTCCloudDef.TRTCSpeedTestResult result) {
        Log.i(TAG, "onSpeedTestResult: 网速测试的结果回调");
    }

    @Override
    public void onConnectionLost() {
        Log.i(TAG, "onConnectionLost: SDK 与云端的连接已经断开");
    }

    @Override
    public void onTryToReconnect() {
        Log.i(TAG, "onTryToReconnect: SDK 正在尝试重新连接到云端");
    }

    @Override
    public void onConnectionRecovery() {
        Log.i(TAG, "onConnectionRecovery: SDK 与云端的连接已经恢复");
    }

    @Override
    public void onCameraDidReady() {
        Log.i(TAG, "onCameraDidReady: 摄像头准备就绪");
    }

    @Override
    public void onMicDidReady() {
        Log.i(TAG, "onMicDidReady: 麦克风准备就绪");
    }

    @Override
    public void onAudioRouteChanged(int newRoute, int oldRoute) {
        Log.i(TAG, "onAudioRouteChanged: 当前音频路由发生变化（仅适用于移动设备）");
    }

    @Override
    public void onUserVideoAvailable(String userId, boolean available) {
        Log.i(TAG, "onUserVideoAvailable: 音量大小的反馈回调");
    }

    @Override
    public void onStartPublishing(int err, String errMsg) {
        Log.i(TAG, "onStartPublishing: 开始向腾讯云直播 CDN 上发布音视频流的事件回调");
    }

    @Override
    public void onStopPublishing(int err, String errMsg) {
        Log.i(TAG, "onStopPublishing: 停止向腾讯云直播 CDN 上发布音视频流的事件回调");
    }

    @Override
    public void onStartPublishCDNStream(int err, String errMsg) {
        Log.i(TAG, "onStartPublishCDNStream: 开始向非腾讯云 CDN 上发布音视频流的事件回调");
    }

    @Override
    public void onStopPublishCDNStream(int err, String errMsg) {
        Log.i(TAG, "onStopPublishCDNStream: 停止向非腾讯云 CDN 上发布音视频流的事件回调");
    }

    @Override
    public void onSetMixTranscodingConfig(int err, String errMsg) {
        Log.i(TAG, "onSetMixTranscodingConfig: 设置云端混流的排版布局和转码参数的事件回调");
    }

    @Override
    public void onScreenCaptureStarted() {
        Log.i(TAG, "onScreenCaptureStarted: 屏幕分享开启的事件回调");
    }

    @Override
    public void onScreenCapturePaused() {
        Log.i(TAG, "onScreenCapturePaused: 屏幕分享暂停的事件回调");
    }

    @Override
    public void onScreenCaptureResumed() {
        Log.i(TAG, "onScreenCaptureResumed: 屏幕分享恢复的事件回调");
    }

    @Override
    public void onScreenCaptureStopped(int reason) {
        Log.i(TAG, "onScreenCaptureStopped: 屏幕分享停止的事件回调");
    }

    @Override
    public void onLocalRecordBegin(int errCode, String storagePath) {
        Log.i(TAG, "onLocalRecordBegin: 本地录制任务已经开始的事件回调");
    }

    @Override
    public void onLocalRecording(long duration, String storagePath) {
        Log.i(TAG, "onLocalRecording: 本地录制任务正在进行中的进展事件回调");
    }

    @Override
    public void onLocalRecordComplete(int errCode, String storagePath) {
        Log.i(TAG, "onLocalRecordComplete: 本地录制任务已经结束的事件回调");
    }


}
